<?php

namespace app\xinrong\controller;

use app\xinrong\model\XinrongCostModel;
use app\xinrong\model\XinrongMaterialModel;
use app\xinrong\model\XinrongProduceModel;
use app\xinrong\model\XinrongProductModel;
use cmf\controller\AdminBaseController;

class ProductController extends AdminBaseController
{
    public function index()
    {
        $where = [
            'status' => 1
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['name'] = ['like', "%$keyword%"];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['name|area'] = ['like', "%$keyword%"];
        }
        $xinrongProductModel = new XinrongProductModel();
        $lists = $xinrongProductModel
            ->where($where)
            ->order('id desc')
            ->paginate();

        $lists->appends(input());

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    public function restore() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('访问错误！');
        }
        $xinrongProductModel = new XinrongProductModel();
        $info = $xinrongProductModel->find($id);


        $xinrongProductModel->save([
            'status' => 0
        ],[
            'id' => $id
        ]);

        if($info['score'] == 1) {
            $xinrongProduceModel = new XinrongProduceModel();
            $xinrongProduceModel->save([
                'status' => 1
            ],[
                'status' => 2,
                'to_id' => $id,
                'to_type' => 1
            ]);
        }else{
            $xinrongMaterialModel = new XinrongMaterialModel();
            $xinrongMaterialModel->save([
                'status' => 1
            ],[
                'status' => 2,
                'to_id' => $id,
                'to_type' => 1
            ]);
        }
        $this->success('还原成功！');
    }

    public function conversion() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('访问错误！');
        }
        $xinrongProductModel = new XinrongProductModel();
        $result = $xinrongProductModel->find($id);
        if(!$result) {
            $this->error('访问错误！');
        }

        $xinrongMaterialModel = new XinrongMaterialModel();
        $flag = $xinrongMaterialModel->save([
            'name' => $result['name'],
            'reg_time' => $result['reg_time'],
            'cost' => $result['cost'],
            'manager' => cmf_get_current_admin_id(),
            'type' => 1,
            'registered_capital' => $result['registered_capital'],
            'corporation' => $result['corporation'],
            'status' => 1,
            'score' => 1,
        ]);
        if($flag === false) {
            $this->error('转化失败！');
        }

        $xinrongCostModel = new XinrongCostModel();
        $xinrongCostModel->save([
            'cost' => $result['cost'],
            'cid' => 1,
            'tid' => 1,
            'pid' => $xinrongMaterialModel->id,
        ]);

        $xinrongProductModel->save([
            'status' => 2,
            'to_id' => $xinrongMaterialModel->id,
            'to_type' => 1
        ],[
            'id' => $id,
            'status' => 1
        ]);
        $this->success('转化成功！',url('material/index'));
    }

    public function transfer() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('访问错误！');
        }
        $xinrongProductModel = new XinrongProductModel();
        $xinrongProductModel->save(['turnover' => 1],[
            'id' => $id,
            'turnover' => 0,
            'status' => 1
        ]);
        $this->success('转让处理完成！');
    }

    public function turnover() {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('访问错误！');
        }
        $xinrongProductModel = new XinrongProductModel();
        $xinrongProductModel->save(['turnover' => 2],[
            'id' => $id,
            'turnover' => 1,
            'status' => 1
        ]);
        $this->success('转让完成！');
    }
}